﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAO;
using DVO;

public partial class DocumentApprove : System.Web.UI.Page
{
    public int _WId = 0;
    public int _Id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.User.Identity.Name == "")
        {
            Response.Redirect("Login.aspx");
        }
        else
        {
            if (!String.IsNullOrEmpty(Request.QueryString["WId"]))
            {
                _WId = Convert.ToInt32(Request.QueryString["WId"].ToString());
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CId"]))
            {
                _Id = Convert.ToInt32(Request.QueryString["CId"].ToString());
            }
            if (IsPostBack == false)
            {
                if (_WId > 0)
                {
                    LoadData(_WId);
                }
            }
        }
    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        string email = hdfemail.Value ;
        string author = "";
        int id = Convert.ToInt32(hdfcommentId.Value);
        int ApproveId = 0;
        string comment = txtComment.Text.Trim();
        if (!String.IsNullOrEmpty(Page.User.Identity.Name))
        {
            author = Page.User.Identity.Name;
        }
        if (!string.IsNullOrEmpty(Request.QueryString["API"]))
        {
            ApproveId = Convert.ToInt32(Request.QueryString["API"].ToString());
        }
        BLL_Working ObjBLL = new BLL_Working();
        int documentid = Convert.ToInt32(hdfdocumentId.Value);
        int workid = Convert.ToInt32(hdfworkid.Value);
        //ins_Document_SelectByIdResult ObjResult = new ins_Document_SelectByIdResult();
        //ObjResult = ObjBLL.Document_SelectById(_WId);
        //_Id = ObjResult.Id;
        int status = 1;
        if (ObjBLL.Insert_Comment(documentid, comment, author, status, id, workid, ApproveId) > 0)
        {
            if (ckbAttach.Checked == true)
            {
                if (EmailHelper.SendNewMessage(Server.MapPath("~/Resources/Template"), email, "1", _WId) == true)
                {
                    lbmessage.Text = "Gửi tài liệu thành công";
                    return;
                }
                else
                {
                    lbmessage.Text = "Gửi tài liệu thành công";
                    return;
                }

            }
            else
            {
                if (EmailHelper.SendNewMessage(Server.MapPath("~/Resources/Template"), email, "", _WId) == true)
                {
                    lbmessage.Text = "Gửi tài liệu thành công";
                    return;
                }
                else
                {
                    lbmessage.Text = "Gửi tài liệu thành công";
                    return;
                }
            }
        }

    }
    protected void btnfeedback_Click(object sender, EventArgs e)
    {
        if (txtNotes.Text == "")
        {
            lbmessage.Text = "Chưa điền ghi chú!";
            return;
        }
        int documentid = Convert.ToInt32(hdfdocumentId.Value);
        int workid = Convert.ToInt32(hdfworkid.Value);
        int status = 3;
        int ApproveId = 0;
        BLL_Working ObjBLL = new BLL_Working();
        string comment = txtComment.Text.Trim();
        string author = "";
        if (!String.IsNullOrEmpty(Page.User.Identity.Name))
        {
            author = Page.User.Identity.Name;
        }
        if(!string.IsNullOrEmpty(Request.QueryString["API"]))
        {
            ApproveId = Convert.ToInt32(Request.QueryString["API"].ToString());
        }
        int id = Convert.ToInt32(hdfcommentId.Value);
        if (ObjBLL.Insert_Comment(documentid, comment, author, status, id, workid, ApproveId) > 0)
        {
            if (ckbAttach.Checked == true)
            {
                if (EmailHelper.Sendmailfeedback(Server.MapPath("~/Resources/Template"), hdfemail.Value, "1", documentid, txtComment.Text.Trim()) == true)
                {
                    lbmessage.Text = "Giử phản hồi thành công";
                    return;
                }
                else
                {
                    lbmessage.Text = "Giử phản hồi thành công";
                    return;
                }
            }
            else
            {
                if (EmailHelper.Sendmailfeedback(Server.MapPath("~/Resources/Template"), hdfemail.Value , "", documentid, txtComment.Text.Trim()) == true)
                {
                    lbmessage.Text = "Giử phản hồi thành công";
                    return;
                }
                else
                {
                    lbmessage.Text = "Giử phản hồi thành công";
                    return;
                }
            }
        }
        else
        {
            lbmessage.Text = "Giử phản hồi thất bại";
        }
    }

    #region "LoadData"
    private void LoadData(int workId)
    {
        BLL_Document ObjBLL = new BLL_Document();
        ins_Document_SelectByIdResult ObjResult = new ins_Document_SelectByIdResult();
        ObjResult = ObjBLL.Document_SelectById(workId);
        txtDocumentName.Text = ObjResult.DocumentName;
        txtUserName.Text = ObjResult.Author;
        txtWorkName.Text = ObjResult.WorkName;
        txtNotes.Text = ObjResult.Notes;
        Linkdocument.Visible = true;
        Linkdocument.HRef = ObjResult.LinkDocument;
        hdfdocumentId.Value = ObjResult.Id.ToString();
        hdfworkid.Value = workId.ToString();
        hdfemail.Value = ObjResult.Email;
    }

    //private void LoadEmail()
    //{
    //    int Id = 0;
    //    string email = "";
    //    if (!string.IsNullOrEmpty(Request.QueryString["WId"]))
    //    {
    //        Id = Convert.ToInt32(Request.QueryString["WId"].ToString());
    //    }
    //    BLL_Approve ObjBll = new BLL_Approve();
    //    BLL_Member Objmember = new BLL_Member();
    //    List<ins_Member_SelectLevelResult> Objlistmember = new List<ins_Member_SelectLevelResult>();
    //    List<ins_Approve_SelectUserResult> Objlist = new List<ins_Approve_SelectUserResult>();
    //    Objlist = ObjBll.Approve_SelectUser(Id).ToList();
    //    for (int i = 0; i < Objlist.Count; i++)
    //    {
    //        email += Objlist[i].UserName + ";";
    //    }
    //    email = email.Substring(0, email.Length - 1);
    //    try
    //    {
    //        Objlistmember = Objmember.Level_Select(3);
    //        ckbemail.DataSource = Objlistmember;
    //        ckbemail.DataTextField = "UserName";
    //        ckbemail.DataValueField = "Email";
    //        ckbemail.DataBind();
    //        String[] adrr = email.Split(';');
    //        foreach (ListItem item in ckbemail.Items)
    //        {
    //            if (adrr.Contains(item.Text))
    //            {
    //                item.Selected = true;
    //            }
    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //        Common.WriteLog(ex.Message, ex.StackTrace);
    //    }
    //}
    #endregion
}